home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Draw / Sources / DrawPart.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  25.1 KB  |  949 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrawPart.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef DRAWPART_H
  14. #include "DrawPart.h"
  15. #endif
  16.  
  17. #ifndef DRAWFRAME_H
  18. #include "DrawFrame.h"
  19. #endif
  20.  
  21. #ifndef SHAPES_H
  22. #include "Shapes.h"
  23. #endif
  24.  
  25. #ifndef DRAWSELECTION_H
  26. #include "DrawSelection.h"
  27. #endif
  28.  
  29. #ifndef TOOLFRAME_H
  30. #include "ToolFrame.h"
  31. #endif
  32.  
  33. #ifndef TOOLFACET_H
  34. #include "ToolFacet.h"
  35. #endif
  36.  
  37. #ifndef PALETTEFRAME_H
  38. #include "PaletteFrame.h"
  39. #endif
  40.  
  41. #ifndef PALETTEFACET_H
  42. #include "PaletteFacet.h"
  43. #endif
  44.  
  45. // ----- Framework Includes -----
  46.  
  47. #ifndef FWUTIL_H
  48. #include "FWUtil.h"
  49. #endif
  50.  
  51. #ifndef FWWINDOW_H
  52. #include "FWWindow.h"
  53. #endif
  54.  
  55. // ----- OPF Foundation Includes -----
  56.  
  57. #ifndef BCSTOREU_H
  58. #include <BCStoreU.h>
  59. #endif
  60.  
  61. // ----- OpenDoc Includes -----
  62.  
  63. #ifndef _ARBITRAT_
  64. #include <Arbitrat.h>
  65. #endif
  66.  
  67. #ifndef _DRAFT_
  68. #include <Draft.h>
  69. #endif
  70.  
  71. #ifndef _DISPTCH_
  72. #include <Disptch.h>
  73. #endif
  74.  
  75. #ifndef _EXCEPT_
  76. #include <Except.h>
  77. #endif
  78.  
  79. #ifndef _FRAME_
  80. #include <Frame.h>
  81. #endif
  82.  
  83. #ifndef _SHAPE_
  84. #include <Shape.h>
  85. #endif
  86.  
  87. #ifndef _STORAGEU_
  88. #include <StorageU.h>
  89. #endif
  90.  
  91. #ifndef _SUVIEW_
  92. #include <SUView.h>
  93. #endif
  94.  
  95. #ifndef _SUCURSOR_
  96. #include <SUCursor.h>
  97. #endif
  98.  
  99. #ifndef _XMPSESSN_
  100. #include <XMPSessM.h>
  101. #endif
  102.  
  103. #ifndef _INFO_
  104. #include <Info.h>
  105. #endif
  106.  
  107. #ifndef _MENUBAR_
  108. #include <MenuBar.h>
  109. #endif
  110.  
  111. #ifndef _WINDOW_
  112. #include <Window.h>
  113. #endif
  114.  
  115. #ifndef _WINSTAT_
  116. #include <WinStat.h>
  117. #endif
  118.  
  119. #ifndef _TRNSFORM_
  120. #include <Trnsform.h>
  121. #endif
  122.  
  123. #ifndef _SEMTINTF_
  124. #include <SemtIntf.h>
  125. #endif
  126.  
  127. #ifndef _NAMRSLVR_
  128. #include <NamRslvr.h>
  129. #endif
  130.  
  131. #ifndef _STDPROPS_
  132. #include <StdProps.h>
  133. #endif
  134.  
  135. #ifndef _STDTYPES_
  136. #include <StdTypes.h>
  137. #endif
  138.  
  139. #ifndef _DRAGDROP_
  140. #include <DragDrop.h>
  141. #endif
  142.  
  143. #ifndef _CMDDEFS_
  144. #include <CmdDefs.h>
  145. #endif
  146.  
  147. #ifndef _LINKSRC_
  148. #include <LinkSrc.h>
  149. #endif
  150.  
  151. #ifndef _LINK_
  152. #include <Link.h>
  153. #endif
  154.  
  155. #ifndef _PLFMDEF_
  156. #include <PlfmDef.h>
  157. #endif
  158.  
  159. // ----- Macintosh Includes -----
  160.  
  161. #ifndef __EVENTS__
  162. #include <Events.h>        // GetMouse, StillDown.
  163. #endif
  164.  
  165. #ifndef __MENUS__
  166. #include <Menus.h>
  167. #endif
  168.  
  169. #ifndef __QUICKDRAW__
  170. #include <Quickdraw.h>
  171. #endif
  172.  
  173. #ifndef __PICKER__
  174. #include <Picker.h>
  175. #endif
  176.  
  177. #ifndef __TOOLUTILS__
  178. #include <ToolUtils.h>    // HiWord etc.
  179. #endif
  180.  
  181. #ifndef __LIMITS__
  182. #include <limits.h>
  183. #endif
  184.  
  185. #ifndef __STDLIB__
  186. #include <StdLib.h>        // Abs
  187. #endif
  188.  
  189. #ifndef mathRoutinesIncludes
  190. #include <math routines.h>        // See MoveTransformBy
  191. #endif
  192.  
  193. #pragma segment drawpart
  194.  
  195. //=========================================================================
  196. //    •• class CDrawPart
  197. //=========================================================================
  198.  
  199. //------------------------------------------------------------------------------
  200. //    • CDrawPart::CDrawPart
  201. //------------------------------------------------------------------------------
  202.  
  203. CDrawPart::CDrawPart()
  204. {
  205.     fPenSizeMenu = NULL;
  206.     fArrangeMenu = NULL;
  207.     fWindowMenu = NULL;
  208.     
  209.     fShapeList = NULL;
  210.     fCurrentPenSize = ff(1);
  211.     fFillFrame = kFrameFill;
  212.     
  213.     fNbEmbedded = 0;
  214.     
  215.     fPaletteWindow = NULL;
  216.     fToolsWindow = NULL;
  217.     fTool = kRectangle;
  218.     
  219.     fDrawSelection = NULL;
  220.     
  221.     fFillColor = FW_kRGBWhite;
  222.     fPenColor = FW_kRGBBlack;
  223. }
  224.  
  225. //------------------------------------------------------------------------------
  226. //    • CDrawPart::~CDrawPart
  227. //------------------------------------------------------------------------------
  228.  
  229. CDrawPart::~CDrawPart()
  230. {
  231.     if (fPenSizeMenu) 
  232.         ::DisposeMenu(fPenSizeMenu);
  233.     
  234.     if (fArrangeMenu) 
  235.         ::DisposeMenu(fArrangeMenu);
  236.  
  237.     if (fWindowMenu) 
  238.         ::DisposeMenu(fWindowMenu);
  239.     
  240.     delete fShapeList;
  241.     fShapeList = NULL;
  242. }
  243.  
  244. //------------------------------------------------------------------------------
  245. //    • CDrawPart::Initialize
  246. //------------------------------------------------------------------------------
  247.  
  248. void CDrawPart::Initialize()
  249. {
  250.     FW_CEmbeddingPart::Initialize();
  251.     
  252.     fShapeList = new BC_TUnboundedCollection<CBaseShape*, BC_CUnmanaged>;
  253.  
  254.     fArrangeMenu = ::NewMenu(kArrangeMenu,"\pArrange");
  255.     fPenSizeMenu = ::NewMenu(kPenSizeMenu,"\pSize");
  256.     fWindowMenu = ::NewMenu(kWindowMenu,"\pWindows");
  257.  
  258.     ::AppendMenu(fPenSizeMenu,"\p1 Pixel");
  259.     ::AppendMenu(fPenSizeMenu,"\p2 Pixels");
  260.     ::AppendMenu(fPenSizeMenu,"\p3 Pixels");
  261.     ::AppendMenu(fPenSizeMenu,"\p4 Pixels");
  262.     ::AppendMenu(fPenSizeMenu,"\p5 Pixels");
  263.  
  264.     ::AppendMenu(fArrangeMenu,"\pMove Forward /F");
  265.     ::AppendMenu(fArrangeMenu,"\pMove To Front");
  266.     ::AppendMenu(fArrangeMenu,"\pMove Backward /B");
  267.     ::AppendMenu(fArrangeMenu,"\pMove To Back");
  268.     ::AppendMenu(fArrangeMenu,"\p-");
  269.     ::AppendMenu(fArrangeMenu,"\pFreeze");
  270.     ::AppendMenu(fArrangeMenu,"\pDefrost");
  271.  
  272.     ::AppendMenu(fWindowMenu,"\pabc/P");    // will be changed in AdjustMenus
  273.     ::AppendMenu(fWindowMenu,"\pabc/T");    // will be changed in AdjustMenus
  274.  
  275.     XMPMenuBar *menuBar = GetMenuBar();
  276.  
  277.     menuBar->AddMenuLast(kArrangeMenu, fArrangeMenu, this);
  278.     menuBar->AddMenuLast(kPenSizeMenu, fPenSizeMenu, this);
  279.     menuBar->AddMenuLast(kWindowMenu, fWindowMenu, this);
  280.  
  281.     menuBar->RegisterCommand(cMoveForward, kArrangeMenu, 1);
  282.     menuBar->RegisterCommand(cMoveToFront, kArrangeMenu, 2);
  283.     menuBar->RegisterCommand(cMoveBackward, kArrangeMenu, 3);
  284.     menuBar->RegisterCommand(cMoveToBack, kArrangeMenu, 4);
  285.     menuBar->RegisterCommand(cFreeze, kArrangeMenu, 6);
  286.     menuBar->RegisterCommand(cDefrost, kArrangeMenu, 7);
  287.     
  288.     menuBar->RegisterCommand(cPen1, kPenSizeMenu, 1);
  289.     menuBar->RegisterCommand(cPen2, kPenSizeMenu, 2);
  290.     menuBar->RegisterCommand(cPen3, kPenSizeMenu, 3);
  291.     menuBar->RegisterCommand(cPen4, kPenSizeMenu, 4);
  292.     menuBar->RegisterCommand(cPen5, kPenSizeMenu, 5);
  293.  
  294.     menuBar->RegisterCommand(cHideShowPalette, kWindowMenu, 1);
  295.     menuBar->RegisterCommand(cHideShowTools, kWindowMenu, 2);
  296.     
  297.     // ----- Tokenize -----
  298.     fToolPresentation = GetSession()->Tokenize(kToolPresentation);
  299.     fPalettePresentation = GetSession()->Tokenize(kPalettePresentation);
  300.     fMainPresentation = GetSession()->Tokenize(kMainPresentation);
  301.     
  302.     // ----- Create tool window object
  303.     FW_CRect toolRect(ff(100), ff(100), ff(100+kToolsWindowWidth), ff(100 + kToolsWindowHeight));
  304.     fToolsWindow = new FW_CFloatingWindow;
  305.     fToolsWindow->InitFloatingWindow(this, fToolPresentation, toolRect);
  306.  
  307.     // ----- Create palette window object
  308.     FW_CRect paletteRect(ff(100), ff(100), ff(100+kPaletteWindowWidth), ff(100+kPaletteWindowHeight));
  309.     fPaletteWindow = new FW_CFloatingWindow;
  310.     fPaletteWindow->InitFloatingWindow(this, fPalettePresentation, paletteRect);
  311. }
  312.  
  313. //------------------------------------------------------------------------------
  314. //    • CDrawPart::AddProperties
  315. //------------------------------------------------------------------------------
  316.  
  317. void CDrawPart::AddProperties(XMPStorageUnit* storageUnit)
  318. {
  319.     storageUnit->AddProperty(kContentAnnotationProp)->AddValue(kContentAnnotationValue);
  320. }
  321.  
  322. //------------------------------------------------------------------------------
  323. //    • CDrawPart::InternalizeContent
  324. //------------------------------------------------------------------------------
  325.  
  326. void CDrawPart::InternalizeContent(XMPStorageUnit* storageUnit)
  327. {
  328.     storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  329.  
  330.     // ----- Read number of shapes -----
  331.     unsigned long count;
  332.     storageUnit->GetValue(sizeof(count), (XMPValue)&count);
  333.  
  334.     // ----- Read top left offset -----
  335.     XMPCoordinate xTopLeft, yTopLeft;
  336.     storageUnit->GetValue(sizeof(xTopLeft), (XMPValue)&xTopLeft);
  337.     storageUnit->GetValue(sizeof(yTopLeft), (XMPValue)&yTopLeft);
  338.  
  339.     for (unsigned long i = 0; i<count; i++)
  340.     {
  341.         unsigned short shapeType;
  342.         storageUnit->GetValue(sizeof(unsigned short), (XMPValue)&shapeType);    
  343.         CBaseShape* theShape = NewShape(shapeType);
  344.         if (theShape)
  345.         {
  346.             theShape->Unflatten(this, storageUnit);            
  347.             fShapeList->Append(theShape);
  348.             
  349.             if (theShape->IsSelected())
  350.                 fDrawSelection->AddToSelection(theShape, TRUE);
  351.                 
  352.             if (xTopLeft != 0 || yTopLeft != 0)
  353.             {
  354.                 theShape->OffsetShape(xTopLeft, yTopLeft);
  355.             }
  356.         }
  357.     }
  358.  
  359.     // ----- Read general information -----
  360.     if (storageUnit->Exists(kContentAnnotationProp, (XMPValueType)kContentAnnotationValue, 0))
  361.     {
  362.         storageUnit->Focus(kContentAnnotationProp, kXMPPosUndefined, kContentAnnotationValue, (XMPValueIndex)0, kXMPPosUndefined);
  363.         storageUnit->GetValue(sizeof(unsigned short), (XMPValue)&fTool);
  364.         storageUnit->GetValue(sizeof(XMPCoordinate), (XMPValue)&fCurrentPenSize);
  365.         storageUnit->GetValue(sizeof(unsigned short), (XMPValue)&fFillFrame);
  366.     }
  367. }
  368.  
  369. //------------------------------------------------------------------------------
  370. //    • CDrawPart::ExternalizeContent
  371. //------------------------------------------------------------------------------
  372.  
  373. void CDrawPart::ExternalizeContent(XMPStorageUnit* storageUnit)
  374. {
  375.     storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  376.     storageUnit->Remove();
  377.     storageUnit->AddValue(this->GetContentPropertyValueType());
  378.     
  379.     // ----- Write number of shapes -----
  380.     unsigned long count = fShapeList->Length();
  381.     storageUnit->SetValue(sizeof(count), (XMPValue)&count);
  382.     
  383.     // ----- Write top left offset -----
  384.     XMPCoordinate coordinate = 0;
  385.     storageUnit->SetValue(sizeof(coordinate), (XMPValue)&coordinate);
  386.     storageUnit->SetValue(sizeof(coordinate), (XMPValue)&coordinate);
  387.  
  388.     // ----- Write shapes -----
  389.     BC_TCollectionActiveIterator<CBaseShape*> ite(*fShapeList);
  390.     while (!ite.IsDone())
  391.     {
  392.         (*ite.CurrentItem())->Flatten(storageUnit);
  393.         ite.Next();
  394.     }    
  395.         
  396.     // ----- Write general information -----
  397.     storageUnit->Focus(kContentAnnotationProp, kXMPPosUndefined, kContentAnnotationValue, (XMPValueIndex)0, kXMPPosUndefined);
  398.     storageUnit->SetValue(sizeof(unsigned short), (XMPValue)&fTool);
  399.     storageUnit->SetValue(sizeof(XMPCoordinate), (XMPValue)&fCurrentPenSize);
  400.     storageUnit->SetValue(sizeof(unsigned short), (XMPValue)&fFillFrame);
  401. }
  402.  
  403. //------------------------------------------------------------------------------
  404. //    • CDrawPart::NewShape
  405. //------------------------------------------------------------------------------
  406.  
  407. CBaseShape* CDrawPart::NewShape(unsigned short shapeType)
  408. {    
  409.     CBaseShape *theShape = NULL;
  410.     
  411.     switch (shapeType) {
  412.         case kLineShape:
  413.             theShape = new CLineShape();
  414.             break;
  415.         case kRectShape:
  416.             theShape = new CRectShape();
  417.             break;
  418.         case kOvalShape:
  419.             theShape = new COvalShape();
  420.             break;
  421.         case kRRectShape:
  422.             theShape = new CRoundRectShape();
  423.             break;
  424.         case kProxyShape:
  425.             theShape = new CProxyShape();
  426.             break;
  427.     }
  428.     
  429.     return theShape;
  430. }
  431.  
  432. //------------------------------------------------------------------------------
  433. //    • CDrawPart::Release
  434. //------------------------------------------------------------------------------
  435.  
  436. void CDrawPart::Release()
  437. {    
  438.     if (fShapeList && this->GetRefCount() == 1)
  439.     {
  440.         CBaseShape* shape;
  441.         while (!fShapeList->IsEmpty())
  442.         {
  443.             shape = fShapeList->First();
  444.             fShapeList->Remove(fShapeList->Location(shape));
  445.             delete shape;
  446.         }
  447.     }
  448.     
  449.     FW_CEmbeddingPart::Release();
  450. }
  451.  
  452.  
  453. //------------------------------------------------------------------------------
  454. //    • CDrawPart::NewSelection
  455. //------------------------------------------------------------------------------
  456. //    We keep a reference to the selection around to avoid
  457. //    typecast. fDrawSelection will be deleted by FW_CPart
  458.  
  459. FW_CSelection* CDrawPart::NewSelection()
  460. {    
  461.     fDrawSelection = new CDrawSelection;
  462.     fDrawSelection->InitDrawSelection(this);
  463.     return fDrawSelection;
  464. }
  465.  
  466. //------------------------------------------------------------------------------
  467. //    • CDrawPart::DeleteShape
  468. //------------------------------------------------------------------------------
  469. void CDrawPart::DeleteShape(CBaseShape* shape)
  470. {
  471.     fShapeList->Remove(fShapeList->Location(shape));
  472.     shape->Removed();
  473.     delete shape;
  474. }
  475.  
  476. //------------------------------------------------------------------------------
  477. //    • CDrawPart::AddShape
  478. //------------------------------------------------------------------------------
  479. void CDrawPart::AddShape(CBaseShape* shape)
  480. {
  481.     fShapeList->Insert(shape, 0);
  482. }
  483.  
  484. //------------------------------------------------------------------------------
  485. //    • CDrawPart::AddShapeAfter
  486. //------------------------------------------------------------------------------
  487. void CDrawPart::AddShapeAfter(CBaseShape* after, CBaseShape* shape)
  488. {
  489.     fShapeList->Append(shape, fShapeList->Location(after));
  490. }
  491.  
  492. //------------------------------------------------------------------------------
  493. //    • CDrawPart::GetContentPropertyValueType
  494. //------------------------------------------------------------------------------
  495. XMPValueType CDrawPart::GetContentPropertyValueType() const
  496. {
  497.     return kDrawKind;
  498. }
  499.  
  500. //------------------------------------------------------------------------------
  501. //    • CDrawPart::DoMenuEvent
  502. //------------------------------------------------------------------------------
  503. FW_Boolean CDrawPart::DoMenuEvent(XMPMenuBar *menuBar, XMPCommandID commandID)
  504. {
  505.     FW_Boolean result = TRUE;
  506.     
  507.     if (cPen1 <= commandID && commandID <= cPen5)
  508.     {
  509.         XMPCoordinate penSize = ff(commandID - cPen1 + 1);
  510.         if (!fDrawSelection->IsEmpty())
  511.             fDrawSelection->ChangeSelectionPenSize(penSize);
  512.         else
  513.             fCurrentPenSize = penSize;
  514.     }
  515.     else
  516.     {
  517.         switch (commandID)
  518.         {
  519.             case cHideShowPalette:
  520.                 fPaletteWindow->HideShowFloating();
  521.                 break;
  522.             case cHideShowTools:
  523.                 fToolsWindow->HideShowFloating();
  524.                 break;
  525.             case cMoveForward:
  526.                 MoveForward();
  527.                 this->Changed();
  528.                 break;
  529.             case cMoveToFront:
  530.                 MoveToFront();
  531.                 this->Changed();
  532.                 break;
  533.             case cMoveBackward:
  534.                 MoveBackward();
  535.                 this->Changed();
  536.                 break;
  537.             case cMoveToBack:
  538.                 MoveToBack();
  539.                 this->Changed();
  540.                 break;
  541.             case cFreeze:
  542.                 fDrawSelection->SetFrozen(TRUE);
  543.                 this->Changed();
  544.                 break;
  545.             case cDefrost:
  546.                 fDrawSelection->SetFrozen(FALSE);
  547.                 this->Changed();
  548.                 break;
  549.             default:
  550.                 result = FW_CEmbeddingPart::DoMenuEvent(menuBar, commandID);
  551.         }    
  552.     }
  553.     
  554.     return result;
  555. }
  556.  
  557. //------------------------------------------------------------------------------
  558. //    • CDrawPart::SetTool
  559. //------------------------------------------------------------------------------
  560. void CDrawPart::SetTool(unsigned short newTool)
  561. {
  562.     if (fTool != newTool)
  563.     {
  564.         if (fTool == kSelectTool && !fDrawSelection->IsEmpty())
  565.         {
  566.             fDrawSelection->CloseSelection();
  567.         }
  568.         
  569.         CheckTool(fTool, newTool);
  570.         fTool = newTool;        
  571.     }
  572. }
  573.  
  574. //------------------------------------------------------------------------------
  575. //    • CDrawPart::SetFillFrame
  576. //------------------------------------------------------------------------------
  577. void CDrawPart::SetFillFrame(unsigned short fillFrame)
  578. {
  579.     if (fFillFrame != fillFrame)
  580.     {        
  581.         if (!fDrawSelection->IsEmpty())
  582.             fDrawSelection->ChangeSelectionFillFrame(fillFrame);
  583.  
  584.         unsigned short oldFillFrame = fFillFrame;
  585.         fFillFrame = fillFrame;        
  586.         CheckFillFrame(oldFillFrame, fFillFrame);
  587.     }
  588. }
  589.  
  590. //------------------------------------------------------------------------------
  591. //    • CDrawPart::NewFrame
  592. //------------------------------------------------------------------------------
  593. FW_CFrame* CDrawPart::NewFrame(XMPFrame* xmpFrame, XMPTypeToken presentation)
  594. {
  595.     if (presentation == fPalettePresentation)
  596.     {
  597.         CPaletteFrame *frame = new CPaletteFrame();
  598.         frame->InitPaletteFrame(xmpFrame, this);
  599.         return frame;
  600.     } 
  601.     else if (presentation == fToolPresentation)
  602.     {
  603.         CToolFrame *frame = new CToolFrame();
  604.         frame->InitToolFrame(xmpFrame, this);
  605.         return frame;
  606.     }
  607.     
  608.     CDrawFrame *frame = new CDrawFrame();
  609.     frame->IDrawFrame(xmpFrame, this);
  610.     return frame;
  611. }
  612.  
  613. //------------------------------------------------------------------------------
  614. //    • CDrawPart::DoAdjustMenus
  615. //------------------------------------------------------------------------------
  616.  
  617. void CDrawPart::DoAdjustMenus(XMPMenuBar* menuBar)
  618. {
  619.     FW_CEmbeddingPart::DoAdjustMenus(menuBar);
  620.     
  621.     XMPCommandID command;
  622.  
  623.     
  624.     // ----- PenSize Menu -----
  625.     XMPCoordinate penSize = fCurrentPenSize;
  626.     if (fDrawSelection->Count() >= 1)
  627.     {
  628.         BC_TCollectionActiveIterator<CBaseShape*> ite(*fDrawSelection->GetSelectionCollection());
  629.         CBaseShape* shape = *ite.CurrentItem();
  630.         penSize = shape->GetPenSize();
  631.     }
  632.     command = FixedToInt(penSize) + cPen1 - 1;
  633.     
  634.     for (XMPCommandID p = cPen1; p <= cPen5; p++)
  635.         menuBar->CheckCommand(p, p == command);
  636.     
  637.     FW_Boolean hasSelection = fDrawSelection->Count()>0;
  638.     
  639.     // ----- Edit Menu -----
  640.     menuBar->EnableCommand(kXMPCommandSelectAll, fShapeList->Length()>0);
  641.     menuBar->EnableCommand(kXMPCommandPaste, HasPropertyOnClipboard(kXMPPropContents, NULL)); // we don't care about the type
  642.     
  643.     // ----- Arrange Menu -----
  644.     menuBar->EnableCommand(cMoveForward, hasSelection);
  645.     menuBar->EnableCommand(cMoveToFront, hasSelection);
  646.     menuBar->EnableCommand(cMoveBackward, hasSelection);
  647.     menuBar->EnableCommand(cMoveToBack, hasSelection);
  648.     menuBar->EnableCommand(cFreeze, fDrawSelection->HasNotFrozen());
  649.     menuBar->EnableCommand(cDefrost, fDrawSelection->HasFrozen());
  650.     
  651.     // ----- Window Menu    
  652.     if (fToolsWindow->IsShown())
  653.         menuBar->SetItemString(cHideShowTools, "\pHide Tools");
  654.     else
  655.         menuBar->SetItemString(cHideShowTools, "\pShow Tools");
  656.  
  657.     if (fPaletteWindow->IsShown())
  658.         menuBar->SetItemString(cHideShowPalette, "\pHide Palette");
  659.     else
  660.         menuBar->SetItemString(cHideShowPalette, "\pShow Palette");
  661. }
  662.  
  663. //------------------------------------------------------------------------------
  664. //    • CDrawPart::NewProxyShape
  665. //------------------------------------------------------------------------------
  666.  
  667. CProxyShape* CDrawPart::NewProxyShape(XMPPart* xmpPart, const FW_CRect& shapeRect, FW_Boolean invalidate)
  668. {    
  669.     // ----- decide on a position -----
  670.     fNbEmbedded++;
  671.     short offset = 40 + fNbEmbedded*20;
  672.     FW_CPoint temp(ff(offset), ff(offset));
  673.     XMPTransform* externalXForm = ::NewXMPTransform(temp);
  674.     
  675.     FW_CRect tempRect = shapeRect;
  676.     XMPShape *frameShape = ::NewXMPShape(tempRect);
  677.     
  678.     // ----- Create the shape
  679.     tempRect.Offset(ff(offset), ff(offset));
  680.     CProxyShape* theShape = new CProxyShape(tempRect);
  681.  
  682.     // ----- Add the shape to the list of shape -----
  683.     AddShape(theShape);
  684.     
  685.     // ----- Create the proxyRun -----
  686.     CDrawProxyRun* proxyRun = (CDrawProxyRun*)NewProxyRun();
  687.     
  688.     // ----- Set the proxyRun of the shape and the shape of the proxyRun -----
  689.     theShape->SetProxyRun(proxyRun);
  690.     proxyRun->SetShape(theShape);
  691.     
  692.     // ----- Embed the part -----
  693.     EmbedPart(xmpPart, proxyRun, fMainPresentation, 
  694.               GetSession()->Tokenize(kXMPViewAsFrame), kNoPresentation,
  695.               externalXForm, frameShape, invalidate);
  696.  
  697.     // ----- Select the selection tool -----
  698.     SetTool(kSelectTool);
  699.     
  700.     return theShape;
  701. }
  702.  
  703. //------------------------------------------------------------------------------
  704. //    • CDrawPart::NewProxyRun
  705. //------------------------------------------------------------------------------
  706.  
  707. FW_CProxyRun* CDrawPart::NewProxyRun()
  708. {
  709.     CDrawProxyRun *proxyRun = new CDrawProxyRun();
  710.     proxyRun->InitDrawProxyRun(this);
  711.     
  712.     return proxyRun;
  713. }
  714.  
  715. //------------------------------------------------------------------------------
  716. //    • CDrawPart::CheckTool
  717. //------------------------------------------------------------------------------
  718.  
  719. void CDrawPart::CheckTool(unsigned short oldTool, unsigned short newTool)
  720. {
  721.     BC_TCollectionActiveIterator<FW_CFrame*> ite(*this->GetDisplayFrameList());
  722.     FW_CFrame *frame;
  723.     while (!ite.IsDone())
  724.     {
  725.         frame = *ite.CurrentItem();
  726.         if (frame->GetPresentation() == fToolPresentation)
  727.         {
  728.             FW_CFrameFacetIterator ite2(frame);
  729.             while (!ite2.IsDone())
  730.             {
  731.                 ((CToolFacet*)ite2.CurrentItem())->CheckTool(oldTool, newTool);
  732.                 ite2.Next();
  733.             }
  734.         }
  735.         ite.Next();
  736.     }
  737. }
  738.  
  739. //------------------------------------------------------------------------------
  740. //    • CDrawPart::CheckFillFrame
  741. //------------------------------------------------------------------------------
  742.  
  743. void CDrawPart::CheckFillFrame(unsigned short oldFillFrame, unsigned short newFillFrame)
  744. {
  745.     BC_TCollectionActiveIterator<FW_CFrame*> ite(*this->GetDisplayFrameList());
  746.     FW_CFrame *frame;
  747.     while (!ite.IsDone())
  748.     {
  749.         frame = *ite.CurrentItem();
  750.         if (frame->GetPresentation() == fToolPresentation)
  751.         {
  752.             FW_CFrameFacetIterator ite2(frame);
  753.             while (!ite2.IsDone())
  754.             {
  755.                 ((CToolFacet*)ite2.CurrentItem())->CheckFillFrame(oldFillFrame, newFillFrame);
  756.                 ite2.Next();
  757.             }
  758.         }
  759.         ite.Next();
  760.     }
  761. }
  762.  
  763. //------------------------------------------------------------------------------
  764. //    • CDrawPart::MoveForward
  765. //------------------------------------------------------------------------------
  766.  
  767. void CDrawPart::MoveForward()
  768. {
  769.     FW_Boolean didIt = FALSE;
  770.     BC_TCollectionActiveIterator<CBaseShape*> ite(*fDrawSelection->GetSelectionCollection());
  771.     CBaseShape* shape;
  772.     while (!ite.IsDone())
  773.     {
  774.         shape = *ite.CurrentItem();
  775.         BC_Index curIndex = fShapeList->Location(shape);
  776.         if (curIndex >= 1)
  777.         {
  778.             CBaseShape* previous = (*fShapeList)[curIndex-1];
  779.             fShapeList->Remove(curIndex);
  780.             fShapeList->Insert(shape, curIndex-1);
  781.             previous->MovedBefore(shape);
  782.             didIt = TRUE;
  783.         }
  784.         ite.Next();
  785.     }
  786.  
  787.     if (didIt)
  788.     {
  789.         ClipEmbeddedFrames(fMainPresentation);
  790.         InvalidateAllFrames(fMainPresentation, NULL, fDrawSelection->GetUpdateShape());
  791.     }
  792. }
  793.  
  794. //------------------------------------------------------------------------------
  795. //    • CDrawPart::MoveBackward
  796. //------------------------------------------------------------------------------
  797.  
  798. void CDrawPart::MoveBackward()
  799. {
  800.     FW_Boolean didIt = FALSE;
  801.     BC_TCollectionActiveIterator<CBaseShape*> ite(*fDrawSelection->GetSelectionCollection());
  802.     CBaseShape* shape;
  803.     while (!ite.IsDone())
  804.     {
  805.         shape = *ite.CurrentItem();
  806.         BC_Index curIndex = fShapeList->Location(shape);
  807.         if (curIndex < fShapeList->Length() - 1)
  808.         {
  809.             CBaseShape* next = (*fShapeList)[curIndex+1];
  810.             fShapeList->Remove(curIndex);
  811.             fShapeList->Append(shape, curIndex);    // and not curIndex+1 because we removed curIndex
  812.             next->MovedAfter(shape);
  813.             didIt = TRUE;
  814.         }
  815.         ite.Next();
  816.     }
  817.     
  818.     if (didIt)
  819.     {
  820.         ClipEmbeddedFrames(fMainPresentation);
  821.         InvalidateAllFrames(fMainPresentation, NULL, fDrawSelection->GetUpdateShape());
  822.     }
  823. }
  824.  
  825. //------------------------------------------------------------------------------
  826. //    • CDrawPart::MoveToFront
  827. //------------------------------------------------------------------------------
  828.  
  829. void CDrawPart::MoveToFront()
  830. {
  831.     BC_TUnboundedCollection<CBaseShape*, BC_CUnmanaged>* extract = ExtractSelection();
  832.  
  833.     FW_TCollectionActiveIterator<CBaseShape*> ite(*extract, TRUE);    // Iterate backwards
  834.     CBaseShape* shape;
  835.     while (!ite.IsDone())
  836.     {
  837.         shape = *ite.CurrentItem();
  838.         fShapeList->Insert(shape, 0);
  839.         shape->MovedFirst();
  840.         ite.Previous();
  841.     }
  842.     delete extract;
  843.  
  844.     ClipEmbeddedFrames(fMainPresentation);
  845.     InvalidateAllFrames(fMainPresentation, NULL, fDrawSelection->GetUpdateShape());
  846. }
  847.  
  848. //------------------------------------------------------------------------------
  849. //    • CDrawPart::MoveToBack
  850. //------------------------------------------------------------------------------
  851.  
  852. void CDrawPart::MoveToBack()
  853. {
  854.     BC_TUnboundedCollection<CBaseShape*, BC_CUnmanaged>* extract = ExtractSelection();
  855.     
  856.     BC_TCollectionActiveIterator<CBaseShape*> ite(*extract);
  857.     CBaseShape* shape;
  858.     while (!ite.IsDone())
  859.     {
  860.         shape = *ite.CurrentItem();
  861.         fShapeList->Append(shape);
  862.         shape->MovedLast();
  863.         ite.Next();
  864.     }
  865.     delete extract;
  866.  
  867.     ClipEmbeddedFrames(fMainPresentation);
  868.     InvalidateAllFrames(fMainPresentation, NULL, fDrawSelection->GetUpdateShape());
  869. }
  870.  
  871. //------------------------------------------------------------------------------
  872. //    • CDrawPart::ExtractSelection
  873. //------------------------------------------------------------------------------
  874.  
  875. BC_TUnboundedCollection<CBaseShape*, BC_CUnmanaged>* CDrawPart::ExtractSelection()
  876. {
  877.     BC_TUnboundedCollection<CBaseShape*, BC_CUnmanaged>* extract = new BC_TUnboundedCollection<CBaseShape*, BC_CUnmanaged>;
  878.     
  879.     // ----- Create the extract list -----
  880.     BC_TCollectionActiveIterator<CBaseShape*> ite(*fShapeList);
  881.     CBaseShape* shape;
  882.     while (!ite.IsDone())
  883.     {
  884.         shape = *ite.CurrentItem();
  885.         if (shape->IsSelected())
  886.             extract->Append(shape);
  887.         ite.Next();
  888.     }
  889.         
  890.     // ----- Remove then from the shape list -----
  891.     BC_TCollectionActiveIterator<CBaseShape*> ite2(*extract);
  892.     while (!ite2.IsDone())
  893.     {
  894.         fShapeList->Remove(fShapeList->Location(*ite2.CurrentItem()));
  895.         ite2.Next();
  896.     }
  897.     
  898.     return extract;
  899. }
  900.  
  901. //------------------------------------------------------------------------------
  902. //    • CDrawPart::GetDefaultPresentation
  903. //------------------------------------------------------------------------------
  904.  
  905. XMPTypeToken CDrawPart::GetDefaultPresentation() const
  906. {
  907.     return fMainPresentation;
  908. }
  909.  
  910. //------------------------------------------------------------------------------
  911. //    • CDrawPart::SetFillColor
  912. //------------------------------------------------------------------------------
  913.  
  914. void CDrawPart::SetFillColor(const FW_CColor& color)
  915. {
  916.     if (fDrawSelection->IsEmpty())
  917.     {
  918.         fFillColor = color;
  919.         FW_CPartFacetIterator ite(this, GetToolPresentation());
  920.         while (!ite.IsDone())
  921.         {
  922.             ((CToolFacet*)ite.CurrentItem())->UpdateColors();
  923.             ite.Next();
  924.         }
  925.     }
  926.     else
  927.         fDrawSelection->ChangeSelectionFillColor(color);
  928. }
  929.  
  930. //------------------------------------------------------------------------------
  931. //    • CDrawPart::SetPenColor
  932. //------------------------------------------------------------------------------
  933.  
  934. void CDrawPart::SetPenColor(const FW_CColor& color)
  935. {
  936.     if (fDrawSelection->IsEmpty())
  937.     {
  938.         fPenColor = color;
  939.         FW_CPartFacetIterator ite(this, GetToolPresentation());
  940.         while (!ite.IsDone())
  941.         {
  942.             ((CToolFacet*)ite.CurrentItem())->UpdateColors();
  943.             ite.Next();
  944.         }
  945.     }
  946.     else
  947.         fDrawSelection->ChangeSelectionPenColor(color);
  948. }
  949.